package com.cmrx.model.service.impl;

import com.cmrx.model.dao.CommonPictureDao;
import com.cmrx.model.dao.PictureThumbnailDao;
import com.cmrx.model.dao.ScenePhotoDao;
import com.cmrx.model.entity.CommonPicture;
import com.cmrx.model.entity.CommonPictureThumbnail;
import com.cmrx.model.entity.Photo;
import com.cmrx.model.entity.User;
import com.cmrx.model.service.ScenePhotoService;
import com.cmrx.model.util.Html2ImageUtil;
import com.cmrx.model.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by angle on 2019/2/16.
 */
@Service
@Transactional
public class ScenePhotoServiceImpl implements ScenePhotoService {
    @Autowired
    private ScenePhotoDao scenePhotoDao;

    @Autowired
    private CommonPictureDao commonPictureDao;

    @Autowired
    private PictureThumbnailDao pictureThumbnailDao;
    /**
     * 根据id查找现场照片信息
     * @param id
     * @return
     */
    @Override
    public Photo queryById(String id) {
        Photo photo = scenePhotoDao.queryById(id);
        CommonPicture com = commonPictureDao.queryById(photo.getPhotoId());
        photo.setContent(com.getContent());
        return photo;
    }

    /**
     * 根据现勘号查找所有现场图
     * @param investigationId
     * @return
     */
    @Override
    public List<Photo> queryByInvestigationId(String investigationId) {
        List<Photo> photos = scenePhotoDao.queryByInvestigationId(investigationId);
        if(photos.size() > 0){
            List<Photo> newPhotos = new ArrayList<>();
            for(Photo photo:photos){
                CommonPictureThumbnail pictureThumbnail = pictureThumbnailDao.queryByPictureId(photo.getPhotoId());
                photo.setContent(pictureThumbnail.getContent());
                newPhotos.add(photo);
            }
            return newPhotos;
        }
        return null;
    }

    /**
     * 根据id删除现场照片信息
     * @param id
     * @return
     */
    @Override
    public int deleteById(String id) {
        Photo photo = scenePhotoDao.queryById(id);
        pictureThumbnailDao.deleteById(photo.getPhotoId());
        commonPictureDao.deleteById(photo.getPhotoId());
        int result = scenePhotoDao.deleteById(id);
        return result;
    }

    /**
     * 添加现场照片信息
     * @param photo
     * @return
     */
    @Override
    public String addPhoto(Photo photo, User user, CommonPicture commonPicture) {
        /***********************现场图图片信息录入--现场图图片表*************************/
        String pictureid = UUID.randomUUID().toString().replaceAll("-","");
        commonPicture.setId(pictureid);
        commonPicture.setType(photo.getPhotoType());
        commonPicture.setCreateUser(user.getUserName());
        int count = commonPictureDao.addCommonPicture(commonPicture);
        /***********************现场图主表信息录入--现场图信息表*************************/
        String photoId = UUID.randomUUID().toString().replaceAll("-","");
        photo.setId(photoId);
        //照片序号
        Integer serialNo = scenePhotoDao.countByInvestigationId(photo.getInvestigationId());
        if(serialNo==null || "".equals(serialNo)){
            serialNo = 1;
        }
        photo.setSerialNo(serialNo+"");
        //上传用户信息
        photo.setCreateUserId(user.getUserid());
        photo.setCreateUser(user.getUserName());
        //图片信息
        photo.setPhotoId(pictureid);
        photo.setPhotoName(commonPicture.getFileName());
        photo.setPhotoType(commonPicture.getType());
        photo.setDescription(commonPicture.getDescription());
        int count1 =  scenePhotoDao.addPhoto(photo);

        /***********************现场图主表信息录入--现场图缩略图表*************************/
        CommonPictureThumbnail pictureThumbnail = new CommonPictureThumbnail();
        pictureThumbnail.setId(UUID.randomUUID().toString().replaceAll("-",""));
        pictureThumbnail.setPictureId(pictureid);
        pictureThumbnail.setContent(Html2ImageUtil.createThinPicture(commonPicture.getContent()));
        pictureThumbnail.setCreateUser(user.getUserName());
        pictureThumbnailDao.addPictureThumbnail(pictureThumbnail);
        if(count>0&&count1>0){
//            Map<String,String> map = new HashMap<>();
//            map.put("id", photo.getId());
//            map.put("photoId", photo.getPhotoId());
//            return map;
            return photo.getId() + '-' + photo.getPhotoId();
        }else{
            return null;
        }
    }

    /**
     * 修改现场照片信息
     * @param photo
     * @return
     */
    @Override
    public int updateById(Photo photo) {
        return scenePhotoDao.updateById(photo);
    }
}
